package org.springbus.ff.animate;

import org.springbus.ff.conf.FFBaseConf;
import org.springbus.ff.filter.Filter;
import org.springbus.ff.filter.FilterData;
import org.springbus.ff.math.Ease;
import org.springbus.ff.math.Maths;
import org.springbus.ff.util.Utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class RotateRepFilter {

    public static  AniFilter toRotateFilter(FFBaseConf conf) {
        String rotate;
        double from = conf.getFrom();
        Float to = conf.getTo();
        Float time = conf.getTime();
        String type = conf.getType();
        double add = conf.getAdd();
        boolean ing = conf.isIng();
        Float delay = conf.getDelay();
        String ease = "linear";
        time = (float) Utils.floor(time);
        from = Utils.angleToRadian(from,4);
        to = (float) Utils.angleToRadian(to,4);

        BigDecimal ddelay = Maths.accAdd(new BigDecimal(delay), new BigDecimal(time));
        String coodi = "between(t," + delay + "," + ddelay.floatValue() + ")";

        // Is it continuous animation or single easing animation
        if (ing) {
            if (to == null) {
                to = (float) (from + add * time);
            }
            add = Utils.angleToRadian(add,4);
            rotate = from + (add + "*t");
        } else {
            rotate = Ease.getVal(ease, from, to - from, time, delay);
        }

        String elsestr = "if(lte(t,_delay_)," + to + ",_else_)";
        String a = "if(" + coodi + "," + rotate + ",_else_" + to + "_else_)";
        a = Utils.replacePlusMinus(a);

        Map<String, Object> options = new LinkedHashMap<>();
        options.put("a", a);
        options.put("ow", "hypot(iw,ih)");
        options.put("oh", "ow");
        options.put("c", "black@0");
        Filter filter = new Filter("rotate", options);
        FilterData data = new FilterData();
        data.setTime(time);
        data.setDelay(delay);
        data.setElseStr(elsestr);

        return new AniFilter("rotate", filter, type,"object", data);
    }


    /**
     * create new rotate filter
     * if(a<t<b, T1, if(t<c, C1, if(c<t<d, T2, C2)))
     * @private
     */
    public static AniFilter mergeIntoNewRotateFilter(List<AniFilter> tfilters) {


        String elseReg = "_else_";
        String delayReg = "_delay_";
        String elseNelse = "_else_[0-9a-z]*_else_";
        String a = "";
        String elsea = "";
        for (int index = 0; index < tfilters.size(); index++) {
            AniFilter aniFilter = tfilters.get(index);
            FilterData data = aniFilter.getData();
            Float delay = data.getDelay();
            Filter filter = aniFilter.getFilter();
            if (index > 0) {
                elsea = elsea.replaceAll(delayReg, "" + delay).replaceAll(elseReg, filter.getOptionKey("a"));
                a = a.replace(elseNelse, elsea);
            } else {
                a = filter.getOptionKey("a");
                elsea = data.getElseStr();
            }
        }

        a = a.replace(elseReg, "");
        Map<String, Object> op = new LinkedHashMap<>();
        op.put("a", a);
        op.put("ow", "hypot(iw,ih)");
        op.put("oh", "ow");
        op.put("c", "black@0");
        Filter filter = new Filter("rotate", op);
        return new AniFilter("rotate",
                filter,
                "object", "",
                null);

    }

/**
 * Replace placeholder characters in the filter field
 * @private
 */

public static void replaceRotateFilter(AniFilter aniFilter) {
    String elseReg = "_else_";
    Filter filter = aniFilter.getFilter();
    String a = filter.getOptionKey("a").replace(elseReg, "");
    filter.getOptions().put("a", a);
}

}
